package com.lysj.bhp.user.service;

import com.lysj.bhp.investment.domain.Reward;
import com.lysj.bhp.user.base.UserContent;
import com.lysj.bhp.user.domain.BhpcEditLog;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

/**
 * @author wtl
 * @description
 * @create 2018-06-15 14:12
 **/

@Service
@Transactional
public class BhpcService  extends UserContent {



    public Resp updateBhpcInfo(Long userId, BhpcEditLog model) {
        if (ParamUtil.isBlack(userId)) {
            return new Resp(Resp.PARAM_ERROR, "参数错误");
        }
        if (null==model.getAfterBhpcBookKeepBalance()) {
            return new Resp(Resp.PARAM_ERROR, "bhpc真实余额不能为空");
        }
        if (null==model.getAfterBhpcAvailBalance()) {
            return new Resp(Resp.PARAM_ERROR, "bhpc可用余额不能为空");
        }
        if (null==model.getAfterBhpcBalance()) {
            return new Resp(Resp.PARAM_ERROR, "bhpc总额不能为空");
        }
        if (null==model.getAfterBhpcFreezeBalance()) {
            return new Resp(Resp.PARAM_ERROR, "bhpc冻结余额不能为空");
        }
        if (null==model.getAfterRewardAvailBalance()) {
            return new Resp(Resp.PARAM_ERROR, "可用奖励不能为空");
        }
        if (null==model.getAfterRewardBalance()) {
            return new Resp(Resp.PARAM_ERROR, "奖励总额不能为空");
        }
        if (null==model.getAfterRewardFreezeBalance()) {
            return new Resp(Resp.PARAM_ERROR, "冻结奖励不能为空");
        }

        //查询该用户的bhpc钱包
        BhpcWallet bhpcWallet = bhpcWalletRepository.findByUserId(userId);

        BhpcEditLog bhpcEditLog=new BhpcEditLog();



        //更改bhpc钱包信息
        if(null!=bhpcWallet){
            //记录更改前的bhpc钱包信息
            bhpcEditLog.setBeforeBhpcBookKeepBalance(bhpcWallet.getBookKeepBalance());
            bhpcEditLog.setBeforeBhpcAvailBalance(bhpcWallet.getAvailBalance());
            bhpcEditLog.setBeforeBhpcBalance(bhpcWallet.getBalance());
            bhpcEditLog.setBeforeBhpcFreezeBalance(bhpcWallet.getFreezeBalance());

            //修改bhpc钱包余额
            bhpcWallet.setBookKeepBalance(model.getAfterBhpcBookKeepBalance());
            bhpcWallet.setAvailBalance(model.getAfterBhpcAvailBalance());
            bhpcWallet.setBalance(model.getAfterBhpcBalance());
            bhpcWallet.setFreezeBalance(model.getAfterBhpcFreezeBalance());

            //记录更改后的钱包信息
            bhpcEditLog.setAfterBhpcBookKeepBalance(bhpcWallet.getBookKeepBalance());
            bhpcEditLog.setAfterBhpcAvailBalance(bhpcWallet.getAvailBalance());
            bhpcEditLog.setAfterBhpcBalance(bhpcWallet.getBalance());
            bhpcEditLog.setAfterBhpcFreezeBalance(bhpcWallet.getFreezeBalance());

            bhpcWalletRepository.save(bhpcWallet);
        }

        //查询该用户的奖励
        Reward reward = rewardRepository.findByUserId(userId);
        if(null!=reward){
            //记录更改前的奖励信息
            bhpcEditLog.setBeforeRewardAvailBalance(reward.getAvailBalance());
            bhpcEditLog.setBeforeRewardBalance(reward.getBalance());
            bhpcEditLog.setBeforeRewardFreezeBalance(reward.getFreezeBalance());

            //更改奖励信息
            reward.setAvailBalance(model.getAfterRewardAvailBalance());
            reward.setBalance(model.getAfterRewardBalance());
            reward.setFreezeBalance(model.getAfterRewardFreezeBalance());
            rewardRepository.save(reward);

            //记录更改后的奖励信息
            bhpcEditLog.setAfterRewardAvailBalance(reward.getAvailBalance());
            bhpcEditLog.setAfterRewardBalance(reward.getBalance());
            bhpcEditLog.setAfterRewardFreezeBalance(reward.getFreezeBalance());

        }

        User user=userRepository.findOne(userId);
        bhpcEditLog.setEmail(user.getEmail());
        bhpcEditLog.setUserId(userId);
        bhpcEditLog.setEditDate(new Date());
        bhpcEditLogRepository.save(bhpcEditLog);
        return new Resp("修改成功");
    }
}
